<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
</head>
<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addSpec" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除"   onclick="deleteBath()" ><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input  >									
									<button class="btn btn-default" >查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="specList">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->    
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->

  <!-- 模态修改窗口 -->
  <div class="modal fade" id="updateSpec" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel2">规格编辑</h3>
			  </div>
			  <div class="modal-body">
				  <div id="update_id"></div>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal"  onclick="updateInfo()" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>  <!-- 增加窗口 -->
  <div class="modal fade" id="addSpec" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">增加窗口</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>规格名称</td>
						  <td><input  class="form-control"  name="specName"    placeholder="规格名称" >  </td>
					  </tr>
				  </table>

				  <!-- 规格选项 -->
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" title="新建" ><i class="fa fa-file-o"></i> 新增规格选项</button>

				  </div>

				  <table id="specOption" class="table table-bordered table-striped table-hover dataTable">
					  <thead>
					  <tr>
						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
					  </tr>
					  </thead>
					  <tbody>
					  <tr>

						  <td>
							  <input  class="form-control" name="optionName"  placeholder="规格选项">
						  </td>
						  <td>
							  <input  class="form-control"  name="orders" placeholder="排序">
						  </td>
						  <td>
							  <button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
						  </td>
					  </tr>
					  <tr>

						  <td>
							  <input  class="form-control" name="optionName" placeholder="规格选项">
						  </td>
						  <td>
							  <input  class="form-control" name="orders" placeholder="排序">
						  </td>
						  <td>
							  <button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
						  </td>
					  </tr>
					  <tr>
						  <td>
							  <input  class="form-control"  name="optionName" placeholder="规格选项">
						  </td>
						  <td>
							  <input  class="form-control"  name="orders" placeholder="排序">
						  </td>
						  <td>
							  <button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
						  </td>
					  </tr>
					  </tbody>
				  </table>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" onclick="addSpec()" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
	<script>
		//增加
		function addSpec() {
			//对于js来说是没有集合的,定义一个空的数组,js的数组可以动态的扩容
			var optionList =[];
			 var table = $("#specOption");//通过ID选择器获取到这个表格
			 var trs = $(table).find("tr")//查找tr  //所有的对象如果想要编程jquery对象必须使用$包裹
			console.log(table)//调试前端页面,他会帮我们打印前端数据
			console.log(trs)
			//每一行的tr都拿到了,这个时候我们需要遍历它们
			//遍历有两种方式,一种是js的遍历一种是	jquery遍历
			for(i=1;i<trs.length;i++){//第一行是名称不要,所以我们的tr需要从第二行开始遍历
				//这里是js遍历
				var tds =$(trs[i]).find("td");
				var option ={"optionName":$(tds[0]).find("input").val(),"orders":$(tds[1]).find("input").val()}//这个就代表js中的一个对象
				// console.log($(tds[0]).find("input").val())
				// console.log($(tds[1]).find("input").val())
				optionList.push(option);
			}
			 console.log(optionList);
			// JSON.parse()这个是将String转为js对象
			//将数组转成JSON字符串,它就是一个String类型了
			// console.log(JSON.stringify(optionList))//将js对象转换为String
			var optionList1 =JSON.stringify(optionList);
			alert(optionList1)
			//jquery遍历   其中i代表下标,e代表当前循环的元素对象
			// $(trs).each(function (i,e) {
			//
			// })
			$.ajax({
				url:"/specificationController/toAddSpec",
				type:"post",
				dataType:"json",
				async:false,
				data:{specName:$("[name=specName]").val(),optionList:optionList1},
			})
		}
		$(function () {
			findSpec();
		})
		function findSpec() {//查询方法
			$.ajax({
				url:"/specificationController/toFindAll",
				type:"post",
				dataType:"json",
				async:false,
				data:{},
				success:function (result) {
					var result =result.result;
					var r = "";
					r +="<thory>"
					for (var i=0;i<result.length;i++){
						r += '<tr>';
						r += '<td><input type="checkbox"  name="brand" id="check" value="'+result[i].id +'"></td>';
						r += '<td>' + result[i].id + '</td>';
						r += '<td>' + result[i].specName + '</td>';
						r += '<td class="text-center"><input type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateSpec" onclick="echoSpec('+ result[i].id +')" value="修改"></td>';
						r += '</tr>';
					}
					r +="</thory>";
					$("#specList").html(r);
					console.log(result)
				},
				error:function () {
					alert("系统异常")
				}
			})
		}
		//修改
		function echoSpec(id) {
			//回显
				$.ajax({
					url:"/specificationController/echoSpec/" + id,
					dataType:"json",
					type:"get",
					async:false,
					success:function (result1){
						console.log(result1)
						var optionNameArr = result1.result.specification.specName;
						var optionNameArr2 = result1.result.specification.id;
						var optionNameArr1 = result1.result.options;
						alert(optionNameArr1[0].id)
						alert(optionNameArr)
						var table = "<table class='table table-bordered table-striped'  width='800px'><tr>";
						table += "<input type='hidden' id='id' value='"+optionNameArr2+"'>";
						table += "<td>规格名称</td>";
						table += "<td><input  class='form-control' id='specName3' value='"+optionNameArr+"' placeholder='规格名称'></td></tr></table>";
						table += "<div class='btn-group'>";
						table += "<button type='button' class='btn btn-default' title='新建' ><i class='fa fa-file-o'></i> 新增规格选项</button></div>";
						table += "<table id='specTable3' class='table table-bordered table-striped table-hover dataTable'>"
						table += "<thead><tr><th class='sorting'>规格选项</th>"
						table += "<th class='sorting'>排序</th>";
						table += "<th class='sorting'>操作</th></thead>";
						for (var i = 0; i < optionNameArr1.length; i++) {
							table += "<tbody><tr><td><input  class='form-control' value='"+optionNameArr1[i].optionName+"' placeholder='规格选项'></td>";
							table += "<td><input  class='form-control' value='"+optionNameArr1[i].orders+"'  placeholder='排序'></td>"
							table += "<td><button type='button' class='btn btn-default' title='删除' ><i class='fa fa-trash-o'></i> 删除</button></td></tr>"
						}
						table += "</tbody>";
						$("#update_id").html(table);
					},
					error(result1){
						alert("网络异常,请刷新")
					}
				})
		}
		//删除
		function deleteBath() {
			alert("22222")
			// var id = $("#check").val();
			var allBrand = document.getElementsByName("brand")
			var checked = []
			for(i in allBrand){
				if(allBrand[i].checked){
					checked.push(allBrand[i].value)
				}
			}
			if(checked.length <= 0){
				alert("请选择品牌!");
				return
			}
			console.log(checked)
			$.ajax({
				url:"/specificationController/deleteBatch",
				traditional: true,
				data:{"ids" : checked},
				dataType:"json",
				type:"get",
				async:false,
				success:function (result){
					var msg = result.message
					window.location.reload()
					alert(msg)
				},
				error:function (){
					alert("系统错误!")
				}
			})
		}
		//修改
		function updateInfo(){
			alert(1111)
			var optionList = [];
			var trs = $("#specTable3").find("tr");
			for (var i = 1; i < trs.length; i++) {
				var tds = $(trs[i]).find("td");
				var option = {id:$(tds[2]).find("input").val(), specId:$(tds[3]).find("input").val(), optionName:$(tds[0]).find("input").val(), orders:$(tds[1]).find("input").val()}
				optionList.push(option);
			}
			var optionListJson = JSON.stringify(optionList);
			console.log(optionListJson)
			$.ajax({
				url: "/specificationController/updatespec2",
				type: "post",
				dataType: "json",
				data: {id:$("#id").val(),specName:$("#specName3").val(),optionListJson:optionListJson},
				async: true,
				success: function (list){
					console.log(list)
					alert(list.codename);
					window.location.reload();
				},
				error: function (list){
					console.log(list);
					alert("网络错误")
				}
			})
		}
	</script>
</body>
</html>